window.addEventListener("DOMContentLoaded", function() {

	// 原生JavaScript的点击事件：点击后进行ul列表轮播
	let divNavBar01 = document.getElementsByClassName("first-nav-bar");
	let divNavBar02 = document.getElementsByClassName("second-nav-bar");
	clickMoveLeft(divNavBar01);
	clickMoveLeft(divNavBar02);

	function clickMoveLeft(obj, leftMove = true) {
		let resize01 = null,
			resize02 = null;
		if(obj && obj.length !== undefined && obj.length > 1) {
			let len = obj.length;
			//let nextBrother, leftMove01, subIndex;
			for(let i = 0; i < len; i++) {
				obj[i].addEventListener("click", resize01 = function() {
					let nextBrother = this.parentElement.nextElementSibling || this.parentElement.nextSibling;
					let subIndex = parseInt(this.dataset["subIndex"]);
					let leftMove01 = (nextBrother.offsetWidth / nextBrother.children.length) * subIndex;
					nextBrother.style.left = (-leftMove01) + "px";
					console.dir(this.dataset["subIndex"]);
					let navLen = this.parentElement.children.length;
					for(let j = 0; j < navLen; j++) {
						this.parentElement.children[j].getElementsByTagName("a")[0].style.background = "";
						this.parentElement.children[j].getElementsByTagName("a")[0].style.color = "";
					}
					this.getElementsByTagName("a")[0].style.background = "#32cd32";
					this.getElementsByTagName("a")[0].style.color = "white";
					return;
				});

			}
			window.addEventListener("resize", function() {
				for(let i = 0; i < len; i++) {
					obj[i].parentElement.children[0].click();

				}
				resize01();
			});
			window.addEventListener("DOMContentLoaded", function() {
				for(let i = 0; i < len; i++) {
					obj[i].parentElement.children[0].click();

				}
				resize01();
			});
			return;
		}

	}
	// 点击回到顶部
	let toTop = document.getElementById("toTop");
	toTop.addEventListener("click", function() {
		clearInterval(this.set);
		let _this = this;
		let scrolltop = document.body.scrollTop || document.documentElement.scrollTop;
		if(scrolltop > 0) {
			this.set = null;
			let tenth = scrolltop / 20;
			this.set = setInterval(function() {

				//document.documentElement.scrollTop -= tenth;
				window.scrollTo(0, (scrolltop - tenth));
				scrolltop = document.body.scrollTop || document.documentElement.scrollTop;
				if(scrolltop <= 0) {
					clearInterval(_this.set);
					_this.set = null;
					return;
				}
				scrolltop = document.body.scrollTop || document.documentElement.scrollTop;
				tenth = scrolltop / 20;
			}, 20);
			document.addEventListener("wheel" || "mousewheel", function(e) {
				clearInterval(_this.set);
				_this.set = null;
				return;
			}); // 按空格键时会出现一点bug，就是相对于多次点击回到顶部按钮，不太好
		}

	});
	window.addEventListener("scroll", function() {
		let scrolltop = document.body.scrollTop || document.documentElement.scrollTop;
		if(scrolltop > 300) {
			toTop.classList.add("showToTop");
			toTop.children[0].disabled = "";
			return;
		}
		toTop.children[0].disabled = "disabled";
		toTop.classList.remove("showToTop");
	});

	jQuery.noConflict();
	jQuery(function($) {
		
		$(".one-of-passages-and-news-div a").mouseover(function() {
			if($(this).attr("href") || $(this).attr("href") !== "javascript:;") {
				$(this).attr("title", $(this).text());
				return;
			}
			
		});
		latestNewsCarousel($("#latest-news-focus"));

		function latestNewsCarousel(obj) {
			let index = 0;
			let flag = true;
			let childrenLen = obj.find(".focus-img > div.display-flex:first > a").length;
			let leftBrotherDiv = obj.find(".focus-img > div a").eq(0).position().left;
			for(let i = 0; i < childrenLen; i++) {
				obj.find(".carousel-pointer-circle-ul li.display-flex").append($(`<a href="javascript:;"></a>`));
			}

			obj.find(".focus-text > div").mouseover(function() {

				obj.find(".carousel-pointer-circle-ul .display-flex > a").eq($(this).index()).click();

				index = $(this).index();
			});
			obj.mouseover(function() {
				flag = false;
			}).mouseleave(function() {
				flag = true;
			});
			obj.find(".carousel-pointer-circle-ul .display-flex > a").click(function() {
				index = $(this).index();
				leftBrotherDiv = obj.find(".focus-img > div a").eq(index).position().left;

				obj.find(".focus-img > div").stop(true).animate({
						left: -leftBrotherDiv + "px"
					},
					"slow", "swing"
				);
				$(this).css({
					"background-color": "#ffc"

				}).siblings().css({
					"background-color": ""
				});
				obj.find(".focus-text > div").eq(index).css({
					"background-color": "#ffc"

				}).siblings().css({
					"background-color": ""
				});

			});
			setInterval(function() {
				if(flag === true) {
					// 进行轮播图播放
					obj.find(".carousel-pointer-circle-ul .display-flex > a").eq(index).click();
					index++;
					if(index === childrenLen) {
						index = 0;
					}
					return;
				}
				console.log("定时器停止。");
			}, 2200);

			$(window).resize(function() {
				flag = false;
				leftBrotherDiv = obj.find(".focus-img > div a").eq(index).position().left;
				obj.find(".carousel-pointer-circle-ul .display-flex > a").eq(index).click();
				flag = true;
			});
			// 页面显示与隐藏时，控制定时器开启与关闭停止
			toggleFlag();

			function toggleFlag() {
				document.addEventListener("visibilitychange", function() {
					if(document.hidden) {
						// 页面切换到其他地方
						flag = false;
						return;
					}
					flag = true;

				});

			}

		}
		$("#header-left-list >ul.display-flex:first > li").click(function() {
			let ii = $(".one-of-passages-and-news-div").eq($(this).index()).offset().top;
			$("body, html").stop(true).animate({
					scrollTop: ii
				},
				"slow", "swing"
			);

		});
		lookTime($("#look-time .position-absolute").get(0));
	});
	// 生成时间并显示的function
	function lookTime(obj) {
		let theDate = new Date();
		let y1 = 0,
			m1 = 0,
			d1 = 0,
			h1 = 0,
			m2 = 0,
			s1 = 0;
		let weeks = [
			"星期天（Sunday）", "星期一（Monday）",
			"星期二（Tuesday）", "星期三（Wednesday）",
			"星期四（Thursday）", "星期五（Friday）", "星期六（Saturday）"
		];
		let weekDay = "";
		// 以上是代表：年、月、日、时、分、秒 的变量

		(function produceTime() {
			theDate = new Date();
			y1 = theDate.getFullYear();
			m1 = theDate.getMonth() + 1;
			d1 = theDate.getDate();
			h1 = theDate.getHours();
			m2 = theDate.getMinutes();
			s1 = theDate.getSeconds();
			weekDay = weeks[theDate.getDay()];
			h1 = h1 >9 ? h1 : ("0" + h1);
			m2 = m2 >9 ? m2 : ("0" + m2);
			s1 = s1 >9 ? s1 : ("0" + s1);
			obj.innerHTML = `
					${y1 + " 年 " + m1 + " 月 " + d1 + " 日  " + h1 + " : " + m2 + " : " + s1 + "<br />" + weekDay}
				`;
			setTimeout(function(obj) {
				produceTime(obj);

			}, 1000);

		})(obj);
	}
});